package com.longma.server.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.longma.server.constant.enums.ResponseCode;
import com.longma.server.dao.AlarmSettingDAO;
import com.longma.server.entity.*;
import com.longma.server.exception.BusinessException;
import com.longma.server.pojo.dto.AdvanceQueryConditionDTO;
import com.longma.server.pojo.dto.AlarmSettingAddDTO;
import com.longma.server.pojo.dto.AlarmSettingUpdateDTO;
import com.longma.server.service.IAlarmSettingService;
import com.longma.server.util.MybatisPlusPageUtil;
import com.longma.server.util.WrappersUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HAIBO
 * @since 2021-04-28
 */
@Service
@Transactional
public class AlarmSettingServiceImpl  implements IAlarmSettingService {

    @Autowired
    private AlarmSettingDAO alarmSettingDAO;

    /**
     * 新增告警设置
     */
    @Override
    public AlarmSetting addAlarmSetting(AlarmSettingAddDTO addRequestDTO) {
        // 复制
        AlarmSetting alarmSetting = new AlarmSetting();
        BeanUtil.copyProperties(addRequestDTO,alarmSetting);
        alarmSettingDAO.save(alarmSetting);
        return alarmSetting;
    }

    /**
     * 根据告警id删除告警设置
     */
    @Override
    public boolean deleteAlarmSetting(Integer id) {
        if (!isExistId(id)){
            throw new BusinessException(ResponseCode.PARAM_NOT_VALID.setMessage("id不存在"));
        }
        return alarmSettingDAO.removeById(id);
    }

    /**
     * 根据告警id列表删除告警设置
     */
    @Override
    public boolean deleteAlarmSettingByIds(List<Integer> ids) {
        if (!isExistIds(ids)){
            throw new BusinessException(ResponseCode.PARAM_NOT_VALID.setMessage("id不存在"));
        }

        return alarmSettingDAO.removeByIds(ids);
    }


    /**
     * 更新告警设置
     */
    @Override
    public AlarmSetting updateAlarmSetting(AlarmSettingUpdateDTO updateRequestDTO) {
        // 存在校验
        if(alarmSettingDAO.getById(updateRequestDTO.getId()) == null){
            throw new BusinessException(ResponseCode.PARAM_NOT_VALID.setMessage("id不存在"));
        }

        // 复制
        AlarmSetting alarmSetting = new AlarmSetting();
        BeanUtil.copyProperties(updateRequestDTO,alarmSetting);
        alarmSettingDAO.updateById(alarmSetting);
        return alarmSetting;
    }

    /**
     * 分页查询 获取告警设置
     */
    @Override
    public IPage<AlarmSetting> getAlarmSettingByPage(Long pageNum, Long pageSize, Boolean all, List<AdvanceQueryConditionDTO> conditions) {
        LambdaQueryWrapper<AlarmSetting> wrapper = WrappersUtil.lambdaQuery(conditions, AlarmSetting.class);
        Page<AlarmSetting> pageAlarm = MybatisPlusPageUtil.getPage(pageNum, pageSize, all);
        return alarmSettingDAO.page(pageAlarm, wrapper);
    }


    /**
     * 判断id列表是否存在
     */
    private boolean isExistIds(List<Integer> ids) {
        if (CollUtil.isEmpty(ids)) {
            return false;
        }
        LambdaQueryWrapper<AlarmSetting> wrapper = Wrappers.lambdaQuery();
        Set<Integer> productIds = new HashSet<>(ids);
        return alarmSettingDAO.count(wrapper.in(AlarmSetting::getId, productIds)) == productIds.size();
    }

    /**
     * 判断id是否存在
     */
    private boolean isExistId(Integer id) {
        return isExistIds(Arrays.asList(id));
    }
}
